<?php
class feedController extends AController {
	private $tableName = 'ASK';
	
	public function indexAction() {
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$sel['DEP_NO'] = $this->request->getGetParameter('dep');
		$sel['NAME'] = $this->request->getGetParameter('men');
		$sel['bt'] = $this->request->getGetParameter('bt');
		$sel['et'] = $this->request->getGetParameter('et');
		$sel['STATE'] = $this->request->getGetParameter('state');
		$sel['TYPE'] = $this->request->getGetParameter('type');
		if(isset($_POST['sub_sel'])){
			$sel['DEP_NO'] = $this->request->getPostParameter('dep');
			$sel['NAME'] = $this->request->getPostParameter('men');
			$sel['bt'] = $this->request->getPostParameter('bt');
			$sel['et'] = $this->request->getPostParameter('et');
			$sel['STATE'] = $this->request->getPostParameter('state');
			$sel['TYPE'] = $this->request->getPostParameter('type');
		}
		$allConsult = $this->getAllConsult($page,$pagesize,$sel);
		$tolNum = $this->getConsultTolNum($sel);
		$dep = $this->getAllDep();
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->dep = $sel['DEP_NO'];
		$this->men = $sel['NAME'];
		$this->begtime = $sel['bt'];
		$this->endtime = $sel['et'];
		$this->state = $sel['STATE'];
		$this->type = $sel['TYPE'];
		$this->allDep = $dep;
		$this->allconsult = $allConsult;
		$this->tolNum = $tolNum[0]['NUM'];
		return $this->render();
	}
	
	public function zixunListAction(){
		$id = $this->request->getGetParameter('id');
		if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest'){
			$upd['ASK_ID'] = $id;
			$upd['USER_ID'] = CSM_USER_ID;
			$upd['NAME'] = CSM_USER_NAME;
			$upd['CONTENT'] = $this->request->getPostParameter('askcon');
			$upd['DEP_NO'] = $this->request->getPostParameter('dep');
			$upd['DOC_NO'] = $this->request->getPostParameter('doc');
			$upd['UNIT_ID'] = $this->unid;
			$upd['ANSWER_TIME'] = date("Y-m-d H:i:s");
			$upd['STATE'] = 1;
			$state = $this->request->getPostParameter('pid');
			if($state>0){
				if($this->updateAnswer($state,$upd)){
					$this->updConsult($id,array('upd_time'=>date('Y-m-d H:i:s'),'upd_user'=>$this->username,'state'=>2));
					echo json_encode(array('code'=>1000,'msg'=>'suc','pid'=>$state));exit;
				}
				echo json_encode(array('code'=>1001,'msg'=>'fail','pid'=>$state));exit;
			}
			if($pid = $this->insertAnswer($upd)){
				$this->updConsult($id,array('upd_time'=>date('Y-m-d H:i:s'),'upd_user'=>$this->username,'state'=>2));
				echo json_encode(array('code'=>1000,'msg'=>'suc','pid'=>$pid));exit;
			}
			echo json_encode(array('code'=>1001,'msg'=>'fail','pid'=>''));
			exit;
		}
		$askInfo = $this->getAskInfoById($id);
		$answerinfo = $this->getAnswerinfoByAskid($id);
		$dep = $this->getAllDep();
		$allDoc = $this->getAllDoc();
		foreach ($allDoc as $v){
			$doc[$v['DID']] = $v['DN'];
		}
		$this->askinfo = $askInfo;
		$this->answerinfo = $answerinfo;
		$this->id = $id;
		$this->alldep = $dep;
		$this->alldoc = $doc;
		return $this->render();
	}
	// 评价反馈页面
	public function passAction() {
		$pass = $this->request->getGetParameter('p');
		$id = $this->request->getGetParameter('id');
		if($this->updConsult($id,array('state'=>$pass))){
			$msg = array(
				'code'=>1000,
				'msg'=>'suc'
			);
			echo json_encode($msg);exit;
		}
		echo "{'code':1001,'msg':'fail'}";exit;
	}
	public function lookaskAction() {
		$id = $this->request->getGetParameter('id');
		$outid = $this->request->getGetParameter('outid');  //用于区别弹层
		$askInfo = $this->getAskInfoById($id);
		$dep = $this->getAllDep();
		$allDoc = $this->getAllDoc();
		foreach ($allDoc as $v){
			$doc[$v['DID']] = $v['DN'];
		}
		unset($allDoc);
		$this->askinfo = $askInfo;
		$this->alldep = $dep;
		$this->id = $id;
		$this->outid = $outid;
		$this->alldoc = $doc;
		return $this->render();
	}
	public function feedbackAction(){
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$sel['DEP_NO'] = $this->request->getGetParameter('dep');
		$sel['NAME'] = $this->request->getGetParameter('name');
		$sel['bt'] = $this->request->getGetParameter('bt');
		$sel['et'] = $this->request->getGetParameter('et');
		$sel['STATE'] = $this->request->getGetParameter('state');
		$sel['TYPE'] = $this->request->getGetParameter('type');
		if(isset($_POST['back_sel'])){
			$sel['DEP_NO'] = $this->request->getPostParameter('dep');
			$sel['NAME'] = $this->request->getPostParameter('name');
			$sel['bt'] = $this->request->getPostParameter('bt');
			$sel['et'] = $this->request->getPostParameter('et');
			$sel['STATE'] = $this->request->getPostParameter('state');
			$sel['TYPE'] = $this->request->getPostParameter('type');
		}
		$allFeedback = $this->getAllFeedback($page,$pagesize,$sel);
		$tolNum = $this->getFeedbackTolNum($sel);
//		var_dump($allFeedback);
		$dep = $this->getAllDep();
		$states = ATools::getConfig('app.feedback.state');
		$types = ATools::getConfig('app.feedback.type');
		$this->states = $states;
		$this->types = $types;
		$this->alldep = $dep;
		$this->dep = $sel['DEP_NO'];
		$this->state = $sel['STATE'];
		$this->name = $sel['NAME'];
		$this->begtime = $sel['bt'];
		$this->endtime = $sel['et'];
		$this->type = $sel['TYPE'];
		$this->page= $page;
		$this->pagesize = $pagesize;
		$this->tolNum = $tolNum[0]['NUM'];
		$this->allfeedback = $allFeedback;
		return $this->render();
	}
	/**
	 * 编辑咨询信息
	 */
	public function editaskAction() {
		$id = $this->request->getGetParameter('id');
		if(emptY($id)){
			$this->alert('参数不对');exit;
		}
		$row = $this->getAskInfoById($id);
		if(strtotime('-1 days') > strtotime($row['ASK_TIME']) && $_SESSION['user']['work_no']>0){
			$this->alert('对不起，咨询反馈的修改时限为1天，请联系管理员处理，谢谢合作！');exit;
		}
		if(isset($_POST['sub_ask'])){
			$upd['dep_type'] = $this->request->getPostParameter('deptype');
			$upd['dep_no'] = $this->request->getPostParameter('depid');
			$upd['title'] = $this->request->getPostParameter('title');
			$upd['content'] = $this->request->getPostParameter('content');
			$upd['type'] = $this->request->getPostParameter('type');
			$upd['upd_time'] = date('Y-m-d H:i:s');
			$upd['upd_user'] = CSM_USER_ID;
			if(JYDB::update($this->tableName,'ask_id='.$id,$upd)){
				$this->alert('修改成功');exit;
			}
			$this->alert('系统繁忙');exit;
		}
		$this->row = $row;
		return $this->render();
	}
	/**
	 * 编辑咨询信息
	 */
	public function editfeedAction() {
		$id = $this->request->getGetParameter('id');
		if(emptY($id)){
			$this->alert('参数不对');exit;
		}
		$row = $this->getFeedbackById($id);
		if(strtotime('-1 days') > strtotime($row['FEED_TIME']) && $_SESSION['user']['work_no']>0){
			$this->alert('对不起，咨询反馈的修改时限为1天，请联系管理员处理，谢谢合作！');exit;
		}
		if(isset($_POST['sub_ask'])){
			$upd['dep_type'] = $this->request->getPostParameter('deptype');
			$upd['dep_no'] = $this->request->getPostParameter('depid');
			$upd['content'] = $this->request->getPostParameter('content');
			$upd['type'] = $this->request->getPostParameter('type');
			if(JYDB::update('FEEDBACK','f_id='.$id,$upd)){
				$this->alert('修改成功');exit;
			}
			$this->alert('系统繁忙');exit;
		}
		$this->row = $row;
		return $this->render();
	}
	public function delaskAction(){
		$id = $this->request->getGetParameter('id');
		if(emptY($id)){
			$this->alert('参数不对');exit;
		}
		$row = $this->getFeedbackById($id);
		if(strtotime('-1 days') > strtotime($row['FEED_TIME']) && $_SESSION['user']['work_no']>0){
			$this->alert('对不起，咨询反馈的修改时限为1天，请联系管理员处理，谢谢合作！');exit;
		}
		if(JYDB::update($this->tableName,'ask_id='.$id,array('state'=>0))){
			$this->alert('删除成功');exit;
		}
		$this->alert('系统繁忙');exit;
	}
	public function delfeedAction(){
		$id = $this->request->getGetParameter('id');
		if(emptY($id)){
			$this->alert('参数不对');exit;
		}
		$row = $this->getAskInfoById($id);
		if(strtotime('-1 days') > strtotime($row['ASK_TIME']) && $_SESSION['user']['work_no']>0){
			$this->alert('对不起，咨询反馈的修改时限为1天，请联系管理员处理，谢谢合作！');exit;
		}
		if(JYDB::update('FEEDBACK','f_id='.$id,array('state'=>0))){
			$this->alert('删除成功');exit;
		}
		$this->alert('系统繁忙');exit;
	}
	
	public function lookfeedbackAction() {
		$id = $this->request->getGetParameter('id');
		$pid = $this->request->getGetParameter('pid'); //区分回复 与 查看  pid存在看回复，不存在只查看
		$outid = $this->request->getGetParameter('outid'); //用于区别弹层
		if(isset($_POST['sub_handle'])){
			$upd['content'] = $this->request->getPostParameter('handle');
			if(empty($upd['content'])){
				$this->alert('处理内容不能为空，请重新填写！');exit;
			}
			$upd['f_id'] = $id;
			$upd['name'] = $this->username;
			$upd['unit_id'] = $this->unid;
			$upd['DEP_NO'] = $this->unid;
			$upd['DOC_NO'] = $this->unid;
			$upd['handle_time'] = date('Y-m-d H:i:s');
			$upd['state'] = 1;
			$upd['upd_user'] = $this->username;
			$upd['upd_time'] = date('Y-m-d H:i:s');
			if($this->insertHandle($upd)){
				$this->updFeedback($id, array('state'=>6));
			}
		}
		$feedInfo = $this->getFeedbackById($id);
		$dep = $this->getAllDep();
		$allHandle = array();
		if(!empty($pid)){
			$allHandle = $this->getAllHandleByFeedId($id);
		}
		$states = ATools::getConfig('app.feedback.state');
		$types = ATools::getConfig('app.feedback.type');
		$this->states = $states;
		$this->types = $types;
		$this->feedinfo = $feedInfo;
		$this->alldep = $dep;
		$this->id = $id;
		$this->allhandle = $allHandle;
		$this->pid = $pid;
		$this->outid = $outid;
		return $this->render();
	}
	public function passFeedbackAction() {
		$pass = $this->request->getGetParameter('p');
		$id = $this->request->getGetParameter('id');
		if($this->updFeedback($id,array('state'=>$pass))){
			$msg = array(
				'code'=>1000,
				'msg'=>'suc'
			);
			echo json_encode($msg);exit;
		}
		echo "{'code':1001,'msg':'fail'}";exit;
	}
	//用于弹层显示反馈
	public function feedformAction(){
		$uid = $this->request->getGetParameter('uid');
		$name = $this->request->getGetParameter('name');
		$phone = $this->request->getGetParameter('phone');
		$depid = $this->request->getGetParameter('depid');
		$docid = $this->request->getGetParameter('docid');
		if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == strtolower('XMLHttpRequest')){
			$upd['content'] = $this->request->getPostParameter('content');
			if(empty($upd['content'])){
				return json_encode(array('code'=>-1001,'msg'=>'反馈内容不能为空'));
			}else{
				$upd['DEP_NO'] = $this->request->getPostParameter('depid');
				$upd['DEP_TYPE'] = $this->request->getPostParameter('deptype');
				$upd['patient_id'] = $uid;
				$upd['name'] = $name;
				$upd['phone'] = $phone;
				$upd['DOC_NO'] = $docid;
				$upd['type'] = $this->request->getPostParameter('type');
				$upd['state'] = 1;
				$upd['FEED_TIME'] = date('Y-m-d H:i:s');
				$upd['unit_id'] = $this->unid;
				if($this->insertFeedback($upd)){
					return json_encode(array('code'=>1000,'msg'=>'添加反馈成功'));
				}
				return json_encode(array('code'=>-1001,'msg'=>'添加反馈失败'));
			}
		}
		
		$dep = $this->getAllDep();
		$this->alldep = $dep;
		$this->depid = $depid;
		$this->deptype = 2;
		return $this->render();
	}
	//用于弹层显示咨询
	public function askformAction(){
		$uid = $this->request->getGetParameter('uid');
		$name = $this->request->getGetParameter('name');
		$phone = $this->request->getGetParameter('phone');
		$depid = $this->request->getGetParameter('depid');
		$docid = $this->request->getGetParameter('docid');
		if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == strtolower('XMLHttpRequest')){
			$upd['title'] = $this->request->getPostParameter('title');
			$upd['content'] = $this->request->getPostParameter('content');
			if(empty($upd['content']) || empty($upd['content'])){
				return json_encode(array('code'=>-1001,'msg'=>'咨询内容不能为空'));
			}else{
				$upd['DEP_NO'] = $this->request->getPostParameter('depid');
				$upd['DEP_TYPE'] = $this->request->getPostParameter('deptype');
				$upd['patient_id'] = $uid;
				$upd['name'] = $name;
				$upd['phone'] = $phone;
				$upd['DOC_NO'] = $docid;
				$upd['unit_id'] = $this->unid;
				$upd['type'] = $this->request->getPostParameter('type');
				$upd['state'] = 5;
				$upd['ASK_TIME'] = date('Y-m-d H:i:s');
				$upd['ip'] = JYTools::getClientIp();
				$upd['upd_user'] = $this->username;
				$upd['upd_time'] = date('Y-m-d H:i:s');
				if($this->insertAsk($upd)){
					return json_encode(array('code'=>1000,'msg'=>'添加咨询成功'));
				}else{
					return json_encode(array('code'=>-1001,'msg'=>'添加咨询失败'));
				}
			}
		}else{
			$dep = $this->getAllDep();
			$this->alldep = $dep;
			$this->depid = $depid;
			$this->deptype = 2;
			return $this->render();
		}
	}
		
		/**
		 * 返回反馈历史信息  用于弹层显示
		 */
		public function feedhistoryAction() {
			$uid = $this->request->getGetParameter('pid');
			$page = $this->request->getGetParameter('p');
			if (!$page) $page = 1;
			$pageSize = 4;
			$userfeedinfo = $this->getUserFeekInfoByUid($uid,$page,$pageSize);
			$tolNum = $this->getFeedbackTolNum(array('patient_id'=>$uid));
			$dep = $this->getAllDep();
			$states = ATools::getConfig('app.feedback.state');
			$types = ATools::getConfig('app.feedback.type');
			$this->states = $states;
			$this->types = $types;
			$this->alldep = $dep;
			$this->uid = $uid;
			$this->page = $page;
			$this->pagesize = $pageSize;
			$this->feedinfo = $userfeedinfo;
			$this->tolNum = $tolNum[0]['NUM'];
			return $this->render();
		}
		/**
		 * 返回咨询历史信息  用于弹层显示
		 */
		public function askhistoryAction() {
			$pid = $this->request->getGetParameter('pid');
			$page = $this->request->getGetParameter('p');
			if (!$page) $page = 1;
			$pagesize = 4;
			$allConsult = $this->getAllConsult($page,$pagesize,array('patient_id'=>$pid));
			$tolNum = $this->getConsultTolNum(array('patient_id'=>$pid));
			$dep = $this->getAllDep();
			$this->page = $page;
			$this->pagesize = $pagesize;
			$this->pid = $pid;
			$this->allDep = $dep;
			$this->allconsult = $allConsult;
			$this->tolNum = $tolNum[0]['NUM'];
			return $this->render();
		}
	function getAllDep(){
		$allDep = JYDep::getAllDepts($this->unid);
		foreach ($allDep as $v){
			$dep[$v['DEP_NO']] = $v['DEP_NAME'];
		}
		unset($allDep);
		return $dep;
	}
	function getAllDoc(){
		if($this->unid<0){
			$where = '';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		return JYDB::findAll('DOCTOR',$where,'WORK_NO DID,DOCTOR_NAME DN');
	}
	function getAskInfoById($id){
		return JYDB::find($this->tableName,'ask_id='.$id);
	}
	function getFeedbackById($id){
		return JYDB::find('FEEDBACK','F_ID='.$id);
	}
	function getAllHandleByFeedId($id){
		return JYDB::findAll('FEEDBACK_HANDLE','F_ID='.$id,'NAME,UPD_TIME,UPD_USER,HANDLE_TIME,CONTENT');
	}
	function getAnswerinfoByAskid($id){
		return JYDB::find('ANSWER','ask_id='.$id);
	}
	function getAllFeedback($page,$pagesize,$select){
		if($this->unid<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='bt'){
						$where .= ' and FEED_TIME>\''.$v.'\'';
						continue;
					}
					if($k=='et'){
						$where .= ' and FEED_TIME<\''.$v.'\'';
						continue;
					}
					if($k=='NAME'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'=\''.$v.'\'';
				}
			}
		}
		$sql = "select f.F_ID,
					       f.patient_id,
					       f.name,
					       f.phone,
					       f.type,
					       f.DEP_NO,
					       f.state,
					       f.feed_time,
					       f.content,
					       b.upd_time,
					       b.upd_user
		  from (select * from (select rownum nno,t.* from (select * from FEEDBACK where ".$where.") t where rownum<=".$page*$pagesize.") where nno>".($page-1)*$pagesize.") f
		  left join (select f_id,upd_time,upd_user
		               from FEEDBACK_HANDLE
		              where h_id in
		                    (select max(h_id) from FEEDBACK_HANDLE group by f_id)
		              order by upd_time desc) b on f.f_id = b.f_id
		 order by f.f_id";
		$data = JYDB::query($sql);
		return $data;
	}
	function getFeedbackTolNum($select){
		if($this->unid<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		
			if(count($select)>0){
				foreach ($select as $k=>$v){
					if(!empty($v)){
						if($k=='bt'){
							$where .= ' and FEED_TIME>\''.$v.'\'';
							continue;
						}
						if($k=='et'){
							$where .= ' and FEED_TIME<\''.$v.'\'';
							continue;
						}
						if($k=='NAME'){
							$where .= ' and '.$k.' like \'%'.$v.'%\'';
							continue;
						}
						$where .= ' and '.$k.'=\''.$v.'\'';
					}
				}
			}
//			echo $where;
		$sql = "select count(*) num
		  from (select * from FEEDBACK where ".$where.") a
		  left join (select *
		               from FEEDBACK_HANDLE
		              where h_id in
		                    (select max(h_id) from FEEDBACK_HANDLE group by f_id)
		              order by upd_time desc) b on a.f_id = b.f_id
		 order by a.f_id";
		return JYDB::findAll('FEEDBACK',$where,'count(*) num');
	}
	function getAllConsult($page,$pagesize,$select){
		if($this->unid<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='bt'){
						$where .= ' and ASK_TIME>\''.$v.'\'';
						continue;
					}
					if($k=='et'){
						$where .= ' and ASK_TIME<\''.$v.'\'';
						continue;
					}
					if($k=='NAME'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'=\''.$v.'\'';
				}
			}
		}
//		echo $where;
		$order = 'state desc,ASK_TIME desc';
		return JYDB::findAll($this->tableName,$where,'*',$order,$page,$pagesize);
	}
	function getConsultTolNum($select){
		if($this->unid<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
			if(count($select)>0){
				foreach ($select as $k=>$v){
					if(!empty($v)){
						if($k=='bt'){
							$where .= ' and ASK_TIME>\''.$v.'\'';
							continue;
						}
						if($k=='et'){
							$where .= ' and ASK_TIME<\''.$v.'\'';
							continue;
						}
						if($k=='NAME'){
							$where .= ' and '.$k.' like \'%'.$v.'%\'';
							continue;
						}
						$where .= ' and '.$k.'=\''.$v.'\'';
					}
				}
			}
//			echo $where;
		return JYDB::findAll($this->tableName,$where,'count(*) num');
	}
	function getUserFeekInfoByUid($uid,$page,$pagesize){
		$sql = "select f.F_ID,
					       f.patient_id,
					       f.name,
					       f.phone,
					       f.type,
					       f.DEP_NO,
					       f.state,
					       f.feed_time,
					       f.content,
					       b.upd_time,
					       b.upd_user
		  from (select * from (select rownum nno,t.* from (select * from FEEDBACK where patient_id=".$uid.") t where rownum<=".$page*$pagesize.") where nno>".($page-1)*$pagesize.") f
		  left join (select f_id,upd_time,upd_user
		               from FEEDBACK_HANDLE
		              where h_id in
		                    (select max(h_id) from FEEDBACK_HANDLE group by f_id)
		              order by upd_time desc) b on f.f_id = b.f_id
		 order by f.f_id";
		$data = JYDB::query($sql);
		return $data;
	}
	function updFeedback($id,$arr){
		return JYDB::update('FEEDBACK','F_ID='.$id,$arr);
	}
	function updateAnswer($id,$arr){
		return JYDB::update('ANSWER','answer_id='.$id,$arr);
	}
	function updConsult($id,$arr){
		return JYDB::update($this->tableName,'ask_id='.$id,$arr);
	}
	function insertAnswer($arr){
		return JYDB::create('ANSWER',$arr,'answer_id');
	}
	function insertHandle($upd){
		return JYDB::create('FEEDBACK_HANDLE',$upd,'H_ID');
	}
	function insertFeedback($upd){
		return JYDB::create('FEEDBACK',$upd,'F_ID');
	}
	function insertAsk($upd){
		return JYDB::create('ASK',$upd,'ASK_ID');
	}
	
}